
////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek Studios, 2008.
// -------------------------------------------------------------------------
//  File name:  AnimationBrowser.h
//  Created:     27/6/2008 by Timur.
//  Description: 
// -------------------------------------------------------------------------
//  History:
//  07/06/2009 by Xiaomao: Changed from CE_Panel_Animations to AnimationBrowser,
//              to avoid comfusion with CharPanel_Animation
////////////////////////////////////////////////////////////////////////////

#ifndef __AnimationBrowser_h__
#define __AnimationBrowser_h__
#pragma once

#include "ToolbarDialog.h"
#include "ICryAnimation.h"

class CModelViewportCE;
class CCharacterEditor;
class CAnimationGraphDialog;

#define COLOR_GREYED_OUT 0x999999
#define COLOR_BLACK 0x000000
#define COLOR_RED 0x0000FF

struct AnimationBrowser_AnimDesc
{
	CString groupName;
	CString name;
	bool isGroup;
	bool isMorph;
	size_t animSize;
	uint32 numPosKeys;
	uint32 numRotKeys;
	uint32 iLength;
	AnimationBrowser_AnimDesc(const char* groupName, const char* name, bool isGroup, bool isMorph, size_t animSize, uint32 numPosKeys, uint numRotKeys, uint32 iLength):
	groupName(groupName), name(name), isGroup(isGroup), isMorph(isMorph), animSize(animSize), numPosKeys(numPosKeys), numRotKeys(numRotKeys), iLength(iLength) {};
	AnimationBrowser_AnimDesc(const char* groupName, const char* name, bool isGroup, bool isMorph, size_t animSize, uint32 numPosKeys, uint numRotKeys):
	groupName(groupName), name(name), isGroup(isGroup), isMorph(isMorph), animSize(animSize), numPosKeys(numPosKeys), numRotKeys(numRotKeys), iLength(0) {};
};

class AnimationBrowser_AnimRecord : public CXTPReportRecord
{
	DECLARE_DYNAMIC(AnimationBrowser_AnimRecord)

public:
	AnimationBrowser_AnimRecord(AnimationBrowser_AnimRecord* pParent,int icon, 
		const char* toolTips, const AnimationBrowser_AnimDesc& desc);
	bool IsGroup() const { return m_bIsGroup; }
	const CString& GetName() const { return m_name; }
	const CString& GetGroupName() const { return m_groupName; }
	AnimationBrowser_AnimRecord* GetParent() const { return m_pParent; }
protected:
	AnimationBrowser_AnimRecord* m_pParent;
	CString m_groupName;
	CString m_name;
	bool m_bIsGroup;
};

typedef DynArray<DynArray<AnimationBrowser_AnimRecord*>> IntToRecords;

class CAnimationBrowser : public CDialog, public IEditorNotifyListener, public IAnimationStreamingListener
{
public:
	typedef std::vector<CString>		TDSelectedAnimations;
	// ids of attachments in the attachment browser and the combobox
	struct SAttachmentIDs{
		int32 iIndexAttachBrowser;
		int32 iIndexComboBox;
	};


public:
	CAnimationBrowser();
	virtual ~CAnimationBrowser();

	enum 
	{ 
		IDD = IDD_DB_ENTITY,
		IDD_STANDALONE = IDD_ANIMATION_BROWSER,
	};

	BOOL CreateBitmap(CBitmap& bitmap, const int32 width, const int32 height, const BYTE* pData);
	BOOL LoadAndCombineTrueColorImages( CImageList& imageList, std::vector<UINT>& nIDResources, int nIconWidth, int nIconHeight, COLORREF colMaskColor);
	BOOL CreateBitmapToolbarDynamic(CImageList& imageList);
	void SetCharacterEditor(CCharacterEditor *pCE ) {m_pCharacterEditor = pCE;}
	void SetAnimationGraphDialog(CAnimationGraphDialog* pAnimDialog) {m_pAnimationGraphDialog = pAnimDialog;}
	void AttachModelViewportCE(CModelViewportCE* pModelViewCE);
	void DetachModelViewportCE();

	CModelViewportCE* GetModelViewportCE() const
	{
		return m_pModelViewportCE;
	}
	
	void ReloadAnimations();
	void UpdateAnimations(ICharacterInstance* characterInstantce);
	void RemoveAll();

	static CAnimationBrowser*&	GetCurrentInstance();

	bool GetSelectedAnimations( TDSelectedAnimations &anims );

	// Update comboBox
	void UpdateCharacterComboBox(const std::vector<const char*>& attachmentNames, const std::vector<int>& posInAttachBrowser);
	void UpdateCharacterComboBoxSelection(const int ind);
	
	std::vector<SAttachmentIDs>& GetAttachmentIDs(){return m_attachmentIDs;}

	CXTPToolBar* GetToolBar(){return &m_wndToolBar;}
	
	typedef Functor0 OnDblClickCallback;
	void AddOnDblClickCallback(OnDblClickCallback callback)
	{ stl::push_back_unique(m_onDblClickCallbacks, callback); }
	void RemoveOnDblClickCallback(OnDblClickCallback callback)
	{ stl::find_and_erase(m_onDblClickCallbacks, callback); }

	void CloseThis()
	{ OnClose(); }

protected:
	virtual BOOL OnInitDialog();
	virtual void OnOK() {};
	virtual void OnCancel() {};
	int32 GetAnimIcon(const int32 nAnimId, IAnimationSet* pAnimations);
	void PlaySelectedAnimations(std::vector<CString>& anims);
	void OnSelectedPMG(const std::vector<CString> anims);
	void ExportCAF2HTR(const string name);

	//////////////////////////////////////////////////////////////////////////
	// Messages
	//////////////////////////////////////////////////////////////////////////
	DECLARE_MESSAGE_MAP()
	afx_msg void OnClose();
	afx_msg void OnSize(UINT nType, int cx, int cy);

	afx_msg void OnReportSelChange( NMHDR * pNotifyStruct, LRESULT *result );
	afx_msg void OnReportItemDblClick( NMHDR * pNotifyStruct, LRESULT *result );
	afx_msg void OnFilterText(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnSelectCharacters(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnSelectMotionFromHistory(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnReportItemRClick( NMHDR* pNotifyStruct, LRESULT* result );
	//////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////
	// IEditorNotifyListener
	//////////////////////////////////////////////////////////////////////////
	virtual void OnEditorNotifyEvent( EEditorNotifyEvent event );
	//////////////////////////////////////////////////////////////////////////

	void UpdateSelectedRecord();
	void UpdateAnimationRecord(const CXTPReportRecord *pRecord, const bool loaded, const bool final);

	void LayOutControls();

	CXTPReportControl m_wndReport;
	CImageList m_imageList;
	CXTPToolBar m_wndToolBar;
	CModelViewportCE* m_pModelViewportCE;
	CCharacterEditor *m_pCharacterEditor;
	CAnimationGraphDialog* m_pAnimationGraphDialog;
	ICharacterInstance *m_pCharacterInstance;

	CString m_filterText;
	CString m_filterTextPrev;

	typedef std::vector<OnDblClickCallback> OnDblClickCallbackList;
	OnDblClickCallbackList m_onDblClickCallbacks;

public:
	// this implements the AnimationStreamingListener functionality
	virtual void NotifyAnimLoaded(const int32 globalAnimId);
	virtual void NotifyAnimUnloaded(const int32 globalAnimId);

private:
	AnimationBrowser_AnimRecord* AppendRecord( AnimationBrowser_AnimRecord* pItemGroup, AnimationBrowser_AnimRecord* pItem, int32 globalAnimId);;
	void ClearRecords();
	const DynArray<AnimationBrowser_AnimRecord*>* GetRecords(const int32 globalAnimId);

	std::vector<SAttachmentIDs> m_attachmentIDs;
	CString GenerateToolTips(IAnimationSet* pAnimations, const int animId);
	bool bSelMotionFromHistory;
	IntToRecords m_animNameToRecordMap;
};


#endif
